package engine.newUi
{
	import engine.control.NxTablePageTurner;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.RoleVo;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.tip.TipsMainLogic;
	import engine.ui.UIManage;
	
	import flash.events.Event;
	
	import newx.com.config.StaticIni;
	import newx.com.control.text.NxLabel;
	import newx.com.control.window.NxWindowEvent;

	/**
	 *下注历史记录 
	 * @author yanghr
	 * 
	 */	
	public class StrideChipLogLogic
	{
		private static var _self:StrideChipLogLogic;
		public static function getInstance():StrideChipLogLogic
		{
			if(_self==null)
			{
				_self=new StrideChipLogLogic();
			}
			return _self;
		}
		private var _ui:StrideChipLogUI;
		public function get ui():StrideChipLogUI 
		{
			if(!_ui)
			{
				_ui=new StrideChipLogUI;
				UIManage.getInstance().initUI(_ui,"StrideChipLogUI");
			}
			return _ui;
		}
		public function StrideChipLogLogic()
		{
		}
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		
		private 	var 	_pageTurner:NxTablePageTurner;
		public function  Init():void
		{
			ui;
			if(ui._pageBg==null)return;
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			
			_ui.chipTable.dataProvider=null;
			
			_pageTurner=new NxTablePageTurner(_ui._pageUpBtn,_ui._pageNextBtn,_ui.chipTable,_ui._pageNum);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_MY_PLEDGE),getRecord);
		}
		/**
		 *得到 已下注tips 
		 * @return 
		 * 
		 */		
		public function getCurrentTipsStr():String
		{
			if(currentName&&currentMoney)
			return StaticIni.getSwfLangStrVar("StrideTips_6",[currentName,currentMoney]);
			return "";
		}
		private var currentName:String;
		private var currentMoney:String;
		/**
		 *int fighttype    天榜地榜
			int site         战场序号
			wstring pledgePlayername    押注玩家
			int pledgeGold  押注金额
			int getGold    获得金额  
		 * @param e
		 * 
		 */		
		private function getRecord(e:CustomMsgEvent):void
		{
			trace("StrideChipLogLogic.getRecord(e)",e.pList);
			_ui.chipTable.dataProvider=null;
			var arr:Array=e.pList;
			var timeTotal:int=0;
			var chipTotal:int;
			var gotTotal:int;
			while(arr.length)
			{
				timeTotal++;
				var type:String=arr.shift()==StrideMainLogic.SKY_LOCAL?"Stride_1":"Stride_2";
				var time:int=arr.shift();
				var timesStr:String=StaticIni.getSwfLangStrVar(type,[StrideMainLogic.getInstance().getTurnFromIndex(time)+1])//type+"第"+StrideMainLogic.getInstance().getTurnFromIndex(arr.shift())+"轮";
				var name:String=arr.shift();
				var chipin:int=arr.shift();
				var m:int=arr.shift();
				var chipStr:String
				if(m==0)
				{
					currentName=name;
					currentMoney=chipin+"";
					chipStr=StaticIni.getSwfLangStrVar("StrideChip_10",[name,chipin])//"下注玩家"+arr.shift()+",下注金额 ："+arr.shift()+",获利:"+arr.shift();
				}
				else
				{
					chipStr=StaticIni.getSwfLangStrVar("StrideChip_1",[name,chipin,m])//"下注玩家"+arr.shift()+",下注金额 ："+arr.shift()+",获利:"+arr.shift();
				}
					
				chipTotal+=chipin;
				gotTotal+=m;
				_ui.chipTable.addLineByIndex([timesStr,chipStr],-1);
			}
			if(!_ui.created)
			{
				_ui.showOrHide();
			}
			_ui.mainChipLab.htmlText=StaticIni.getSwfLangStrVar("StrideChip_2",[timeTotal,chipTotal,gotTotal]);
		}
		public function	BeforeShow():void
		{
			addAllEventListener()
		}
		public function	AfterHide():void
		{
			removeAllEventListener()
		}
		public function addAllEventListener():void
		{
			_pageTurner.addEventListener()
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,close)
		}
		public function removeAllEventListener():void
		{
			_pageTurner.removeEventListener();
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,close)			
		}
		private function close(e:Event):void
		{
			_ui.hide();
		}
		private function addLog(range:String,info:String):void
		{
			_ui.chipTable.addLineByIndex([range,info],0);
		}
	}
}